package cart;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import databaseproject.*;
import business.*;
import data.*;
import java.util.ArrayList;

/**
 *
 * @author Joel Murach
 * @version
 */
public class CartServlet extends HttpServlet
{
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        String productCode = request.getParameter("productCode");
        String quantityAsString = request.getParameter("quantity");
        
        //System.out.println(request.getParameter("productCode"));
        //System.out.println(request.getParameter("quantity"));
        
        HttpSession session = request.getSession();
        
        synchronized(session)
        {
            Cart cart = (Cart) session.getAttribute("cart");
            if (cart == null)
            {
                cart = new Cart();
                session.setAttribute("cart", cart);
            }
            
            //if the user enters a negative or invalid quantity,
            //the quantity is automatically reset to 1.
                       
            ServletContext sc = getServletContext();
            String path = sc.getRealPath("WEB-INF/products.txt");
            
            Products product = ProductIO.getProduct(Integer.parseInt(productCode));
            
            //Products product = products.get(quantity);

            LineItem lineItem = new LineItem();
            lineItem.setProduct(product);
            lineItem.setQuantity(Integer.parseInt(quantityAsString));
            if (Integer.parseInt(quantityAsString) > 0)
                cart.addItem(lineItem);
            else if (Integer.parseInt(quantityAsString) <= 0)
                cart.removeItem(lineItem);

            session.setAttribute("cart", cart);
        }
                       
        String url = "/shoppingcart.jsp";
        RequestDispatcher dispatcher =
                getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }
    
    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }
    
    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo()
    {
        return "Short description";
    }
    // </editor-fold>
}
